import { updateSource } from '@/services/api/api-publicity-source';
import ProForm, {
  DrawerForm,
  ProFormDigit,
  ProFormRadio,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import { Button, Spin, message } from 'antd';
import React from 'react';
import { useAccess } from 'umi';

interface EditSourceProps {
  data: PublicityData.Source;
  onRefurbish: () => void;
}

const EditSource: React.FC<EditSourceProps> = ({ data, onRefurbish }) => {
  const access = useAccess();

  return (
    <DrawerForm
      key="publicity_edit_source_drawer_form"
      title="编辑"
      drawerProps={{
        destroyOnClose: true,
      }}
      trigger={<Button type="primary">编辑</Button>}
      initialValues={{
        parentName: data?.name,
        name: data?.name,
        seq: data?.seq,
        state: data?.state,
        isFolder: data?.isFolder,
        score: data?.score,
        describes: data?.describes,
      }}
      onVisibleChange={(visible) => {}}
      onFinish={async (values) => {
        const response = await updateSource({
          id: data.id,
          ...values,
        });
        if (response.code === 0) {
          message.success('提交成功');
          onRefurbish();
        }
        return true;
      }}
    >
      <Spin
        style={{
          textAlign: 'center',
        }}
        spinning={false}
      >
        <ProForm.Group>
          <ProFormText width="md" name="parentName" label="上级" disabled />
          <ProFormDigit width="md" label="排序" name="seq" min={1} max={100} />
        </ProForm.Group>
        <ProForm.Group>
          <ProFormText
            width="md"
            name="name"
            label="名称"
            rules={[{ required: true, message: '请输入名称' }]}
          />
          <ProFormText width="md" name="score" label="得分" />
          <ProFormTextArea width="xl" name="describes" label="描述" />
        </ProForm.Group>
        <ProForm.Group>
          <ProFormRadio.Group
            name="isFolder"
            label="文件夹"
            options={[
              {
                label: '是',
                value: 1,
              },
              {
                label: '否',
                value: 0,
              },
            ]}
          />
        </ProForm.Group>
        <ProForm.Group>
          <ProFormRadio.Group
            name="state"
            label="状态"
            options={[
              {
                label: '启用',
                value: 1,
              },
              {
                label: '禁用',
                value: 0,
              },
            ]}
          />
        </ProForm.Group>
      </Spin>
    </DrawerForm>
  );
};

export default EditSource;
